"use strict"

const mvc = require("cosjs.mvc");
//const title = '游戏配置';
const model = 'branch/config';

const types = {
    "1": {'key':'news','name':'游戏公告','view':"news",},
    "2": {'key':'payment','name':'现金直购','view':"payment",},
    "8": {'key':'active','name':'经典活动','view':"active",},
    "88":{'key':'activeG','name':'超级活动','view':"activeG",},
    "9": {'key':'supmail','name':'超级邮件','view':"supmail",},
    "100": {'key':'excode','name':'兑换码','view':"excode",},
}

exports.index = async function(){
    //this.debug = true;
    let cid = this.get("cid","int");
    if(!cid){
        return this.error("配置类型不能为空");
    }
    let config = types[cid];
    if(!config){
        return this.error("配置类型不存在");
    }
    let data = {"cid":cid,"key":config["key"]};
    let format = ['config',config['key'],''].join('/')

    data['setting']={};
    let setting = this.model.admin('setting');
    let setting_ret=await setting.find({});
    if(setting_ret){
        data.setting=Object.assign(data.setting,setting_ret);        
    }
    let opensdk = this.model.branch("opensdk",this.branch);
    let ret=await opensdk.mget(["_id","name"]);
    data['sdk'] = {"":"不限制"};
    for(let v of ret){
        let k = v["_id"];
        data['sdk'][k] = v['name'];
    }
    let handle = mvc.library("handle",config["name"],model,format);
    return handle.index.call(this,data);

}
//游戏服务器列表管理
exports.page = function (){
    let cid = this.get('cid', 'int');
    let config = types[cid]||null;
    if(!config){
        return this.error("配置类型不存在");
    }
    let query = {"cid":cid}
    let format = ['config',config['key'],''].join('/');
    let handle = mvc.library("handle",config["name"],model,format);
    return handle.page.call(this,query);
};

exports.save = function () {
    let cid = this.get('cid', 'int');
    let config = types[cid]||null;
    if(!config){
        return this.error("配置类型不存在");
    }

    let format = ['config',config['key'],''].join('/');
    let handle = mvc.library("handle",config["name"],model,format);
    return handle.save.call(this,getPostData);
}

exports.copy = function(){
    this.output = "json";
    let id = this.get('id','string');
    if ( !id ) {
        return this.error('args[id] empty');
    }

    let configModel = this.model.branch('config',this.branch);
    return configModel.get(id).then(ret=>{
        if(!ret){
            return this.error("配置不存在");
        }
        delete ret["_id"];
        ret['status'] = 0;
        return configModel.insert(ret);
    })
}



function getPostData(act,info){
    let cid = this.get('cid', 'int');
    //let config = types[cid];
    let format = mvc.format('branch/config/');
    info['cid'] = cid;
    info['upsert'] = Date.now();
    let timeKey=["STime","ETime"];
    let timeType = info['TType'] >0 ?"int":"time";
    for(let k of timeKey){
        info[k] = this.get(k, timeType,format[k]['val']) ;
    }
    return Transformation(info);
}

function Transformation(info){
    let arrSid = [];
    if(info['sid']){
        arrSid = Array.isArray(info['sid']) ? info['sid'] : info['sid'].split(",");
    }
    info['sid'] = [];
    for(let v of arrSid){
        let sid = parseInt(v);
        if(sid) info['sid'].push(sid);
    }
    return info;
}